<template>
  <div>
    <el-card>
      <!-- 添加角色 -->
      <el-row>
        <el-col style="text-align: right;">
          <el-button type="primary" size="mini" @click="addRoleBtnClick">添加角色</el-button>
        </el-col>
      </el-row>
      <!--角色列表-->
      <el-table :data="roleList" border>
        <el-table-column label="No" type="index"></el-table-column>
        <el-table-column label="角色名" prop="roleName" min-width="200"></el-table-column>
        <el-table-column label="状态" width="300">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.roleStatus" @change="roleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="menuClick(scope.row)">菜单</el-button>
            <el-button type="warning" size="mini" @click="powerClick(scope.row)">权限</el-button>
            <el-button type="danger" size="mini" @click="deleteClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-dialogDrag title="添加角色" :visible.sync="addRoleDialog" width="40%" center @closed="addRoleDialogClosed" @opened="addRoleDialogOpen"
      :close-on-click-modal="false">
      <span style="text-align: center">
        <el-form ref="addRoleFormRef" :model="addRoleForm" :rules="addRoleFormRules" label-position="right"
          label-width="80px">
          <!--角色名称-->
          <el-form-item label="角色名称" prop="roleName" v-on:keyup.enter.native="addRole">
            <el-input ref="roleNameInputRef" v-model="addRoleForm.roleName"></el-input>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="addRole">提交</el-button>
        </div>
      </span>
    </el-dialog>
    <el-dialog v-dialogDrag title="菜单权限" :visible.sync="menuPowerDialog" width="40%" center
      :close-on-click-modal="false">
      <span style="text-align: center">
        <el-tree
          :data="roleMenuList"
          show-checkbox
          node-key="menuRoot"
          ref="roleMenuListTreeRef"
          highlight-current
          :props="roleMenuListTreeProps">
        </el-tree>
        <div>
          <el-button type="primary" @click="editRolePower">提交</el-button>
        </div>
      </span>
    </el-dialog>
    <el-dialog v-dialogDrag title="角色权限" :visible.sync="rolePowerDialog" width="90%" center
      :close-on-click-modal="false">
      <span style="text-align: center">
        <el-table :data="rolePowerList" border>
          <el-table-column label="No" type="index"></el-table-column>
          <el-table-column type="expand">
                <template slot-scope="props">
                  <el-table :data="props.row.rolePowerVOS" border  :header-cell-style="{background:'#b6d5f5',color:'#606266'}">
                    <el-table-column label="权限名称" prop="powerName" min-width="200"></el-table-column>
                    <el-table-column label="权限路径" prop="powerPath" min-width="200"></el-table-column>
                    <el-table-column label="状态" width="150">
                      <template slot-scope="scope">
                        <el-switch v-model="scope.row.isEnable" @change="rolePowerStatusChange(scope.row)"></el-switch>
                      </template>
                    </el-table-column>
                  </el-table>
                </template>
              </el-table-column>
          <el-table-column header-align="center" label="权限分组" prop="powerGroup" min-width="200" align="left"></el-table-column>
        </el-table>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Role',
  data () {
    return {
      roleId: -1,
      roleList: [],
      roleMenuList: [],
      rolePowerList: [],
      addRoleDialog: false,
      menuPowerDialog: false,
      rolePowerDialog: false,
      addRoleForm: {
        roleName: ''
      },
      addRoleFormRules: {
        roleName: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }]
      },
      roleMenuListTreeProps: {
        children: 'children',
        label: 'menuName'
      }
    }
  },
  methods: {
    async getRoleList () {
      const res = await this.$apiUtil.get('role/list', {})
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.roleList = res.data
    },
    async addRole () {
      this.$refs.addRoleFormRef.validate(async valid => {
        if (!valid) return
        const res = await this.$apiUtil.postByForm('role/create', this.addRoleForm)
        console.log(res)
        if (res.code !== '200') {
          return this.$message.error(res.msg)
        }
        this.$message.success('添加角色成功')
        this.getRoleList()
        this.addRoleDialog = false
      })
    },
    addRoleBtnClick () {
      this.addRoleDialog = true
    },
    addRoleDialogClosed () {
      this.$refs.addRoleFormRef.resetFields()
    },
    addRoleDialogOpen () {
      this.$refs.roleNameInputRef.focus()
    },
    async roleStatusChange (row) {
      const res = await this.$apiUtil.postByForm('role/changeStatus', { roleId: row.roleId })
      console.log(res)
      if (res.code !== '200') {
        row.roleStatus = !row.roleStatus
        return this.$message.error(res.msg)
      }
      this.$message.success('修改角色状态成功')
      this.getRoleList()
    },
    async menuClick (row) {
      this.menuPowerDialog = true
      const res = await this.$apiUtil.get('menu/roleMenuPowerList', { roleId: row.roleId })
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.roleMenuList = res.data.children
      this.roleId = row.roleId
      const keys = []
      for (let i = 0, len = this.roleMenuList.length; i < len; i++) {
        if (this.roleMenuList[i].children !== undefined) {
          for (let j = 0, len = this.roleMenuList[i].children.length; j < len; j++) {
            if (this.roleMenuList[i].children[j].isEnable) {
              keys.push(this.roleMenuList[i].children[j].menuRoot)
            }
          }
        } else if (this.roleMenuList[i].isEnable) {
          keys.push(this.roleMenuList[i].menuRoot)
        }
      }
      this.$refs.roleMenuListTreeRef.setCheckedKeys(keys)
    },
    deleteClick (row) {
      this.$alert('确定删除角色:' + row.roleName + ' ! ! !', '提示', {
        confirmButtonText: '确定',
        center: true,
        callback: async action => {
          if (action === 'confirm') {
            const res = await this.$apiUtil.postByForm('role/delete', { roleId: row.roleId })
            console.log(res)
            if (res.code !== '200') {
              return this.$message.error(res.msg)
            }
            this.$message.success('删除角色成功')
            this.getRoleList()
          }
        }
      })
    },
    async editRolePower () {
      const roleMenuPowers = this.$refs.roleMenuListTreeRef.getCheckedKeys().concat(this.$refs.roleMenuListTreeRef.getHalfCheckedKeys())
      const res = await this.$apiUtil.postByJson('role/roleMenuPowerEdit', { roleId: this.roleId, roleMenuPowers: roleMenuPowers })
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.$message.success('编辑菜单权限成功')
      this.menuPowerDialog = false
    },
    async powerClick (row) {
      this.rolePowerDialog = true
      const res = await this.$apiUtil.postByForm('power/rolePowerList', { roleId: row.roleId })
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.rolePowerList = res.data
      this.roleId = row.roleId
    },
    async rolePowerStatusChange (row) {
      const res = await this.$apiUtil.postByForm('role/rolePowerEdit', { roleId: this.roleId, powerName: row.powerName })
      console.log(res)
      if (res.code !== '200') {
        row.isEnable = !row.isEnable
        return this.$message.error(res.msg)
      }
      this.$message.success('修改角色权限成功')
    }
  },
  created () {
    this.getRoleList()
  }
}
</script>

<style lang="less" scoped>
</style>
